<template>
  <!--面包屑导航-->
  <Breadcrumb>
    <template #firstTitle>
      <span>权限管理</span>
    </template>
    <template #secondTitle>
      <span>角色列表</span>
    </template>
  </Breadcrumb>

  <!--卡片视图-->
  <el-card>
    <!--添加角色-->
    <el-row>
      <el-col>
        <el-button type="primary" @click="addOrEditVisible = true">添加角色</el-button>
      </el-col>
    </el-row>
    <!--用户列表-->
    <el-table border stripe :data="rolesList" v-loading="loading">
      <!--展开列-->
      <el-table-column type="expand">
        <template #default="props">
          <el-row
            :class="['bdbottom', index1 === 0 ? 'bdtop' : '', 'vcenter']"
            v-for="(item1, index1) in props.row.children"
            :key="item1.id"
          >
            <!--一级权限-->
            <el-col :span="5">
              <el-tag closable @close="removeRightsById(props.row.id, item1.id)">
                {{ item1.authName }}
              </el-tag>
              <el-icon class="icon-arrow-right"><ArrowRight /></el-icon>
            </el-col>

            <el-col :span="19">
              <!--通过for循环嵌套渲染二级权限-->
              <el-row
                :class="[index2 === 0 ? '' : 'bdtop', 'vcenter']"
                v-for="(item2, index2) in item1.children"
                :key="item2.id"
              >
                <el-col :span="6">
                  <el-tag
                    type="success"
                    closable
                    @close="removeRightsById(props.row.id, item2.id)"
                    >{{ item2.authName }}</el-tag
                  >
                  <el-icon class="icon-arrow-right"><ArrowRight /></el-icon>
                </el-col>

                <el-col :span="18">
                  <!--通过for循环嵌套渲染三级权限-->
                  <el-tag
                    type="warning"
                    v-for="item3 in item2.children"
                    :key="item3.id"
                    closable
                    @close="removeRightsById(props.row.id, item3.id)"
                    >{{ item3.authName }}</el-tag
                  >
                </el-col>
              </el-row>
            </el-col>
          </el-row>
        </template>
      </el-table-column>
      <!--索引列-->
      <el-table-column type="index"></el-table-column>
      <el-table-column prop="roleName" label="角色名称"></el-table-column>
      <el-table-column prop="roleDesc" label="角色描述"></el-table-column>
      <el-table-column label="操作" width="220">
        <template #default="scope">
          <!--编辑按钮-->
          <el-button
            type="primary"
            size="small"
            @click="handleEdit(scope.row)"
            >编辑</el-button
          >
          <!--删除按钮-->
          <el-button
            type="danger"
            size="small"
            @click="handleDelete(scope.row.id)"
            >删除</el-button
          >
          <!--分配权限按钮-->
          <el-button
            type="warning"
            size="small"
            @click="handleSetRights(scope.row)"
            >分配权限</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </el-card>

  <!-- 添加/编辑角色 -->
  <addOrEditRole
    v-model="addOrEditVisible"
    v-model:roleInfo="roleInfo"
    @success="addOrEditSuccess"
  />

  <!-- 分配权限 -->
  <setRights
    v-model="setRightsVisible"
    v-model:roleInfo="roleInfo"
    v-model:rightsTree="rightsTree"
    @success="setRightsSuccess"
  />
</template>

<script setup>
import { ref, onMounted } from "vue";
import { ArrowRight } from "@element-plus/icons-vue";
import { getRoles, deleteRoles, getRightsTree, deleteRightsById } from "@/api/roles.js";
import { ElMessage, ElMessageBox } from "element-plus";
import addOrEditRole from "./addOrEditRole.vue";
import setRights from "./setRights.vue";

const rolesList = ref([]); // 列表数据
const loading = ref(false);
const addOrEditVisible = ref(false); // 控制添加用户对话框的显示和隐藏
const setRightsVisible = ref(false); // 控制分配角色对话框的显示与隐藏
const roleInfo = ref({}); // 当前用户信息
const rightsTree = ref([]); // 权限树

onMounted(() => {
  getListData();
  getRightsTreeList();
});

// 获取权限树
const getRightsTreeList = async () => {
  const data = await getRightsTree();
  rightsTree.value = data;
};

// 查询列表
const getListData = async () => {
  loading.value = true;
  const data = await getRoles().finally(() => {
    loading.value = false;
  });
  rolesList.value = data;
};

// 点击编辑用户
const handleEdit = (row) => {
  roleInfo.value = {
    id: row.id,
    roleName: row.roleName,
    roleDesc: row.roleDesc,
  };
  addOrEditVisible.value = true;
};

// 删除
const handleDelete = (id) => {
  ElMessageBox.confirm("此操作将永久删除该角色数据, 是否继续?", "删除角色信息", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    await deleteRoles(id);
    ElMessage.success("删除成功");
    getListData();
  });
};

// 分配权限
const handleSetRights = (row) => {
  roleInfo.value = row;
  setRightsVisible.value = true;
};

// 根据id删除权限
const removeRightsById = (roleId, rightsId) => {
  ElMessageBox.confirm("此操作将永久删除该权限, 是否继续?", "删除权限", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    await deleteRightsById(roleId, rightsId);
    ElMessage.success("删除成功");
    getListData();
  });
};

// 添加/编辑弹框关闭后回调函数
const addOrEditSuccess = () => {
  addOrEditVisible.value = false;
  roleInfo.value = {};
  getListData();
};

// 分配权限弹框关闭后回调函数
const setRightsSuccess = () => {
  setRightsVisible.value = false;
  roleInfo.value = {};
  getListData();
};
</script>

<style lang="scss" scoped>
.el-tag {
  margin: 8px;
}
.icon-arrow-right {
  position: relative;
  top: 3px;
}
.bdtop {
  border-top: 1px solid #eeeeee;
}
.bdbottom {
  border-bottom: 1px solid #eeeeee;
}
.vcenter {
  display: flex;
  align-items: center;
}
</style>
